#ifndef QTLOGBROWSER_H
#define QTLOGBROWSER_H

#include <QPlainTextEdit>
#include <QSyntaxHighlighter>
#include <QTextCharFormat>
#include <QRegularExpression>
#include <QTextDocument>

class LogHighLighter;
class LineNumberArea;
class QtLogBrowser : public QPlainTextEdit
{
    Q_OBJECT
public:
    explicit QtLogBrowser(QWidget *parent = nullptr);

    void lineNumberAreaPaintEvent(QPaintEvent *event);
    int lineNumberAreaWidth();

protected:
    void resizeEvent(QResizeEvent *event) override;

private slots:
    void updateLineNumberAreaWidth(int newBlockCount);
    void highlightCurrentLine();
    void updateLineNumberArea(const QRect &rect, int dy);

private:
    LineNumberArea *lineNumberArea;
    LogHighLighter *highLighter;
};

class LineNumberArea : public QWidget
{
public:
    LineNumberArea(QtLogBrowser *brower) : QWidget(brower), brower(brower)
    {}

    QSize sizeHint() const override
    {
        return QSize(brower->lineNumberAreaWidth() + 10, 0);
    }

protected:
    void paintEvent(QPaintEvent *event) override
    {
        brower->lineNumberAreaPaintEvent(event);
    }

private:
    QtLogBrowser *brower;
};

class LogHighLighter : public QSyntaxHighlighter
{
    Q_OBJECT

public:
    LogHighLighter(QTextDocument *parent = 0);

protected:
    void highlightBlock(const QString &text) override;

private:
    struct HighlightingRule
    {
        QRegularExpression pattern;
        QTextCharFormat format;
    };
    QVector<HighlightingRule> highlightingRules;
};

#endif // QTLOGBROWSER_H
